package com.bnz.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

//@WebFilter(urlPatterns = "/*")
public class CharacterEncodingFilter implements Filter {
    //1. 初始化方法，只执行一次
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterEncodingFilter--->init() ");
    }

    //2.每次请求都会执行，执行多次
    @Override
    public void doFilter(ServletRequest Request, ServletResponse response, FilterChain Chain) throws IOException, ServletException {
        System.out.println("CharacterEncodingFilter--->doFilter() ");
        //2.1 处理post请求中文乱码
        Request.setCharacterEncoding("utf-8");
        //2.2 处理响应的页面字符集编码
        response.setContentType("text/html;charset=utf8");
        //2.3 放行过滤器
        Chain.doFilter(Request,response);
    }

    //3. 销毁过滤器时执行，只执行一次
    @Override
    public void destroy() {
        System.out.println("CharacterEncodingFilter--->destroy() ");
    }
}
